<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学年" prop="stuYearId">
        <!-- <el-input v-model="queryParams.stuId" placeholder="请输入学生id" clearable @keyup.enter.native="handleQuery" /> -->
        <el-select v-model="queryParams.stuYearId" placeholder="请选择学年" filterable clearable>
          <el-option v-for="item in stu_year_list" :key="item.id" :label="item.stuYearName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学院" prop="deptId">
        <el-select @change="changeDept" v-model="queryParams.deptId" placeholder="请选择学院" filterable clearable>
          <el-option v-for="(v, i) in deptList" :key="i" :label="v.label" :value="v.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="专业" prop="majorId">
        <el-select @change="changeMajor" v-model="queryParams.majorId" placeholder="请先选择学院再选择专业" filterable clearable>
          <el-option v-for="(v, i) in majorList" :key="i" :label="v.majorName" :value="v.majorId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="班级" prop="classId">
        <el-select v-model="queryParams.classId" placeholder="请先选择专业再选择班级" filterable clearable>
          <el-option v-for="(v, i) in classList" :key="i" :label="v.className" :value="v.classId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="年级" prop="gradeId">
        <el-select v-model="queryParams.gradeId" placeholder="请选择年级" filterable clearable>
          <el-option v-for="(v, i) in gradeList" :key="i" :label="v.gradeName" :value="v.gradeId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学号" prop="stuNo">
        <el-input v-model="queryParams.stuNo" placeholder="请输入学号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="姓名" prop="stuName">
        <el-input v-model="queryParams.stuName" placeholder="请输入学生姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>


      <!-- <el-form-item label="综合素质成绩" prop="cqScore">
        <el-input v-model="queryParams.cqScore" placeholder="请输入综合素质成绩" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:cqScore:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:cqScore:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:cqScore:remove']">删除</el-button>
      </el-col>
      -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:cqScore:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="initCQVClick">生成综合素质成绩</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableData" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="学年" align="center" prop="stuYearName" width="180" v-if="columns[0].visible" />
      <el-table-column label="学院" align="center" prop="deptName" width="150" v-if="columns[1].visible" />
      <el-table-column label="专业" align="center" prop="majorName" width="280" v-if="columns[2].visible" />
      <el-table-column label="班级" align="center" prop="className" width="280" v-if="columns[3].visible" />
      <!-- <el-table-column label="年级" align="center" prop="gradeName" width="100" /> -->
      <el-table-column label="学号" align="center" prop="stuNo" v-if="columns[4].visible" />
      <el-table-column label="学生姓名" align="center" prop="stuName" v-if="columns[5].visible" />
      <el-table-column label="综合素质成绩" align="center" prop="score" v-if="columns[6].visible" />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:cqScore:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:cqScore:remove']">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改学生综合素质评分表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生id" prop="stuId">
          <el-input v-model="form.stuId" placeholder="请输入学生id" />
        </el-form-item>
        <el-form-item label="学年id" prop="stuYearId">
          <el-input v-model="form.stuYearId" placeholder="请输入学年id" />
        </el-form-item>
        <el-form-item label="综合素质成绩" prop="cqScore">
          <el-input v-model="form.cqScore" placeholder="请输入综合素质成绩" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="根据学年生成综合素质评价成绩" :visible.sync="initCQV" width="450px">
      <el-select style="width: 400px;" v-model="stuYearId" placeholder="请选择学年">
        <el-option :key="i" v-for="(v, i) in stu_year_list" :value="v.id" :label="v.stuYearName"></el-option>
      </el-select>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="initCQ">确 定</el-button>
        <el-button @click="initCQV = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>
  
<script>
import { listCqScore, getCqScore, delCqScore, addCqScore, updateCqScore, initCQ, listCphScore, syncCqScore } from "@/api/stuCQS/info-fill/cqScore";
import { listAllYear as listYear } from "@/api/stuCQS/basedata/year";
import { getDeptName } from "@/api/system/dept";
import { listMajors } from "@/api/stuCQS/basedata/majors";
import { listGrade } from "@/api/stuCQS/basedata/grade";
import { listClass } from "@/api/stuCQS/basedata/class";
import { isEmpty } from "@/api/helpFunc";


export default {
  name: "CqScore",
  data() {
    return {
      columns: [
        { key: 0, label: `学年`, visible: true },
        { key: 1, label: `院系`, visible: true },
        { key: 2, label: `专业`, visible: true },
        { key: 3, label: `班级`, visible: true },
        { key: 4, label: `学号`, visible: true },
        { key: 5, label: `学生姓名`, visible: true },
        { key: 6, label: `综合素质成绩`, visible: true }
      ],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生综合素质评分表表格数据
      cqScoreList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stuId: null,
        stuYearId: null,
        deptId: null,
        majorId: null,
        classId: null,
        gradeId: null,
        stuName: null,
        stuNo: null,


      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },

      //生成成绩
      initCQV: false,
      //学年列表
      stu_year_list: [],
      stuYearId: null,

      tableData: [],
      deptList: [],//学院列表
      majorList: [],//专业列表
      classList: [],//班级列表
      gradeList: [],//年级列表
    };
  },
  created() {
    //this.getList();
    this.getStuYear();
    this.listDept();
    this.listGrade();
    this.loading = false;
  },
  methods: {
    async listGrade() {
      let res = await listGrade();
      this.gradeList = [...res.rows];
    },

    async changeMajor() {
      let sdata = {
        pageNum: 1,
        pageSize: 100,
        majorId : this.queryParams.majorId
      }
      let res = await listClass(sdata);
      this.classList = [...res.rows];
    },
    async changeDept() {
      let sdata = {
        collegeId: this.queryParams.deptId,
        pageNum: 1,
        pageSize: 100
      }
      let res = await listMajors(sdata);
      this.majorList = [...res.rows];
    },
    async listDept() {
      let res = await getDeptName();
      this.deptList = [...res.data];
      console.log(res.data);
    },

    async initCQ() {
      if (this.stuYearId == null) {
        this.$message("请选择学年");
        return;
      }
      this.$message.info("已启动生成");
      this.initCQV = false;
      let res = await syncCqScore(this.stuYearId);
      this.$modal.msgSuccess(res.msg);
      this.initCQV = false;
    },
    initCQVClick() {
      this.initCQV = true;
    },

    //列出学年信息
    async getStuYear() {
      let res = await listYear();
      this.stu_year_list = [...res.data];
    },

    /** 查询学生综合素质评分表列表 */
    getList() {
      this.loading = true;
      listCphScore(this.queryParams).then(response => {
        this.tableData = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stuId: null,
        stuYearId: null,
        cqScore: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生综合素质评分表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCqScore(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生综合素质评分表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCqScore(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCqScore(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学生综合素质评分表编号为"' + ids + '"的数据项？').then(function () {
        return delCqScore(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/cqScore/export', {
        ...this.queryParams
      }, `综合素质成绩${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
  